// pages/qiye_jieshao/qiye_jieshao.js
const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');

var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    url: 'https://smyl.qingkangqingyang.com/api/miaozhimini/weixin/fileUpload',
    typeList: [],
    typeListName: [],
    showaddress: '请选择',
    province: '',
    provinceCode: '',
    cityCode: '',
    areaCode: '',
    businessTime: '',
    description: '',
    lon: '',
    lat: '',
    idCardImgZ: '../../images/add_img.png',
    idCardImgF: '../../images/add_img.png',
    imgSrc: '../../images/add_img.png',
    imgsSrc: [],
    businessImg: '../../images/add_img.png',
    contactName: '',
    contactPhone: '',
    address: '请选择',
    title: '',
    userinfo: [],
    has: 0,
    status: 0,
    remark: '',
    id: 0,
    typeName: '请选择',
    type: 0,
    region: ["请选择地址",'','']
  },
  onLoad() {
    this.setData({
      userinfo: wx.getStorageSync('user')
    });
    this.getinfo();
  },
  orderInfo() {
    wx.navigateTo({
      url: '/pages/mallOrderList/mallOrderList?id='+this.data.id
    });
  },
  showPicker(e) {
    const type = e.currentTarget.dataset.type;
    
  },
  bindPickerChange: function(e) {
    const selectedItem = this.data.typeList[e.detail.value];
    this.setData({
      typeName: selectedItem.label,
      type: selectedItem.value
    });
  },
  bindRegionChange: function (e) {
    var value = e.detail.value; 
    var code = e.detail.code; 
    var postcode = e.detail.postcode;
    this.setData({
        region: e.detail.value,
        province: value[0],
        provinceCode: code[0],
        city: value[1],
        cityCode: code[1],
        area: value[2],
        areaCode: code[2],
    })
},
  
  getinfo() {
    var that = this;
    util.httpRrquest(app.data.apiurl + 'mallshop/getMyshop', 'GET')
    .then(function (response) {
        const data = response.data.data;
        console.log(data);
        var typename = [];
        for (let i = 0; i < data.type.length; i++) {
            typename.push(data.type[i].label);
        }
        that.setData({
          typeList: data.type,
          typeListName:typename
        });
        if (data.has === 1) {
          const shopData = data.data;
          that.setData({
            title: shopData.title,
            contactPhone: shopData.contactPhone,
            address: shopData.address,
            showaddress: shopData.province + shopData.city + shopData.area,
            contactName: shopData.contactName,
            idCardImgF: shopData.idCardImgF,
            idCardImgZ: shopData.idCardImgZ,
            imgSrc: shopData.imgSrc,
            imgsSrc: shopData.imgsSrc,
            businessImg: shopData.businessImg,
            status: shopData.status,
            province: shopData.province,
            provinceCode: shopData.provinceCode,
            city: shopData.city,
            cityCode: shopData.cityCode,
            area: shopData.area,
            areaCode: shopData.areaCode,
            description: shopData.description,
            businessTime: shopData.businessTime,
            remark: shopData.remark ? shopData.remark : '无',
            id: shopData.id,
            type: shopData.cateId,
            typeName: shopData.cname
          });
        }
    }, function (error) {
        console.log(error);
    });
  },
  chooseImage(e) {
    const n = e.currentTarget.dataset.n;
    var that = this;
    wx.chooseImage({
      count: 6,
      sourceType: ['album'],
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '上传中'
        });
        wx.uploadFile({
          url: that.data.url,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {},
          success: (uploadFileRes) => {
            wx.hideLoading();
            const resultData = JSON.parse(uploadFileRes.data).data;
            if (n == 1) {
              that.setData({
                imgSrc: resultData
              });
            } else if (n == 2) {
              const newImgsSrc = that.data.imgsSrc.concat(resultData);
              that.setData({
                imgsSrc: newImgsSrc
              });
            } else if (n == 3) {
              that.setData({
                businessImg: resultData
              });
            } else if (n == 4) {
              that.setData({
                idCardImgZ: resultData
              });
            } else if (n == 5) {
              that.setData({
                idCardImgF: resultData
              });
            }
          }
        });
      }
    });
  },
  del(e) {
    const index = e.currentTarget.dataset.index;
    const newImgsSrc = this.data.imgsSrc;
    newImgsSrc.splice(index, 1);
    this.setData({
      imgsSrc: newImgsSrc
    });
  },
  chooseLocation() {
    wx.chooseLocation({
      success: (res) => {
        this.setData({
          address: res.address + "-" + res.name,
          lat: res.latitude,
          lon: res.longitude
        });
      }
    });
  },
  subinfo() {
    const {
      address,
      title,
      contactName,
      contactPhone,
      businessTime,
      description,
      imgSrc,
      imgsSrc,
      businessImg
    } = this.data;
    if (!address) {
      wx.showToast({
        icon: 'none',
        title: '请选择地址'
      });
      return;
    }
    if (!title) {
      wx.showToast({
        icon: 'none',
        title: '请填写店铺名称'
      });
      return;
    }
    if (!contactName) {
      wx.showToast({
        icon: 'none',
        title: '请填写联系人'
      });
      return;
    }
    if (!contactPhone) {
      wx.showToast({
        icon: 'none',
        title: '请填写联系电话'
      });
      return;
    }
    if (!businessTime) {
      wx.showToast({
        icon: 'none',
        title: '请填写营业时间'
      });
      return;
    }
    if (!description) {
      wx.showToast({
        icon: 'none',
        title: '请填写店铺简介'
      });
      return;
    }
    if (!imgSrc) {
      wx.showToast({
        icon: 'none',
        title: '请上传门头照片'
      });
      return;
    }
    if (!imgsSrc.length) {
      wx.showToast({
        icon: 'none',
        title: '请上传店铺内照片'
      });
      return;
    }
    if (!businessImg) {
      wx.showToast({
        icon: 'none',
        title: '请上传营业执照'
      });
      return;
    }
    var that=this;
    const imgsSrcstr = imgsSrc.join('|');
    wx.showModal({
      title: '提示',
      content: '确认提交申请吗？',
      success: (res) => {
        if (res.confirm) {
            let postdata = {
                  address:that.data.address,
                  lon: that.data.lon,
                  lat: that.data.lat,
                  imgSrc,
                  businessImg,
                  contactName,
                  title,
                  contactPhone,
                  imgsSrc: imgsSrcstr,
                  province: that.data.province,
                  provinceCode: that.data.provinceCode,
                  city: that.data.city,
                  cityCode: that.data.cityCode,
                  area: that.data.area,
                  areaCode: that.data.areaCode,
                  businessTime,
                  description,
                  idCardImgZ: that.data.idCardImgZ,
                  idCardImgF: that.data.idCardImgF,
                  type: that.data.type
            };
            util.httpRrquest(app.data.apiurl + 'mallshop/addShopApply', 'POST',postdata)
            .then(function (response) {
                if (response.data.code == -1) {
                    wx.showToast({
                        icon: 'none',
                        title: response.data.msg
                    });
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: '提交成功，请耐心等待审核！'
                    });
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 1500);
                }
            }, function (error) {
                console.log(error);
            });
        }
      }
    });
  },
  handleInputTitle(e) {
    this.setData({
      title: e.detail.value
    });
  },
  handleInputBusinessTime(e) {
    this.setData({
      businessTime: e.detail.value
    });
  },
  handleInputDescription(e) {
    this.setData({
      description: e.detail.value
    });
  },
  handleInputContactName(e) {
    this.setData({
      contactName: e.detail.value
    });
  },
  handleInputContactPhone(e) {
    this.setData({
      contactPhone: e.detail.value
    });
  }
});